package com.cn.study0013;

import com.cn.study0014.test003.CustomerBiz;

import java.util.Random;
import java.util.Scanner;

/**
 * @author lw
 * @ClassName Menu.java
 * @Description
 * @createTime 2023年05月17日 17:53:00
 */
public class Menu {
    Scanner sc = new Scanner(System.in);
    User user = new User();
    String s = "\\d{2}/\\d{2}";
    CustomerBiz cus = new CustomerBiz();
    Goods goods = new Goods();

    /**
     * 显示登录菜单
     */
    public void showLoginMeun() {
        System.out.println("\n\t\t     欢迎使用我行我素购物管理系统\n");
        System.out.println("\t\t\t1.登录系统\n");
        System.out.println("\t\t\t2.退出系统\n");
        System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
        System.out.print("\n请选择，输入字数：");
    }

    /**
     * 登录验证
     */
    public void LoginMeun1() {
        showLoginMeun();
        int i = 0;
        do {
            if (sc.hasNextInt()) {
                i = sc.nextInt();
                if (i == 1) {
                    for (int j = 1; j <= 3; j++) {
                        showLoginMeun();
                        System.out.print("\n请输入用户名：");
                        String n = sc.next();
                        System.out.print("请输入密码：");
                        String p = sc.next();
                        if (user.name.equals(n) && user.password.equals(p)) {
                            System.out.println("登录成功！" + user.name);
                            do {
                                showMainMeun();
                                i = sc.nextInt();
                                if (i == 0) {
                                    break;
                                } else if (i == 1) {
                                    showCustMeun();     //客户信息管理
                                    continue;
                                } else if (i == 2) {
                                    showMainMeun2();    //购物结算
                                    continue;
                                } else if (i == 3) {
                                    showSendGMeun();    //真情回馈
                                    continue;
                                } else if (i == 4) {
                                    goods.show();        //商品批发城
                                } else if (i == 5) {
                                    i = -1;
                                    break;
                                } else {
                                    System.out.println("输入错误！");
                                    i = -1;
                                    break;
                                }
                        } while (true) ;
                    } else{
                        System.out.println("您没有权限进入系统，请重新登陆！");
                        continue;
                    }
                    break;
                }
                i = -1;
            } else if (i == 2) {
                break;
            }
        } else{
            System.out.println("输入字符不合法！");
            break;
        }
    } while(i !=-1);
        System.out.println("退出系统！");
}

    /**
     * 显示登录菜单
     */
    public void showMainMeun() {
        System.out.println("\n\t\t欢迎使用我行我素购物管理系统\n");
        System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
        System.out.println("\t\t 1.客户信息管理\n");
        System.out.println("\t\t 2.购物结算\n");
        System.out.println("\t\t 3.真情回馈\n");
        System.out.println("\t\t 4.商品批发城");
        System.out.println("\t\t 5.注销\n");
        System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
        System.out.print("\n请选择，输入数字或0返回上一级菜单:");
    }


    /**
     * 购物清算
     */
    public void showMainMeun2() {
        int i = 0;              //商品编号
        String str = null;
        int num = 0;           //购买数量
        String s = null;       //商品名
        double price = 0;      //商品价格
        double total = 0;      //合计
        double dis = 0.8;      //折扣
        System.out.println("我行我素购物管理系统> 购物清算\n");
        System.out.println("* * * * * * * * * * * * * * * * * * * * * ");
        System.out.println("请选择购买的商品编号：");
        System.out.println("1.T恤\t");
        System.out.println("2.网球鞋\t");
        System.out.println("3.网球拍\t");
        System.out.println("* * * * * * * * * * * * * * * * * * * * * \n");
        do {
            System.out.println("请输入商品编号：");
            if (sc.hasNextInt()) {
                i = sc.nextInt();
            }
            System.out.println("请输入购买数量：");
            if (sc.hasNextInt()) {
                num = sc.nextInt();
            }
            switch (i) {
                case 1:
                    s = "T恤";
                    price = 245;
                    break;
                case 2:
                    s = "网球鞋";
                    price = 570;
                    break;
                case 3:
                    s = "网球拍";
                    price = 320;
                    break;
                default:
                    System.out.println("输入错误，没有该商品！");
                    s = "";
            }
            if (!s.equals(null) && !s.equals("")) {
                total = total + price * num;
                System.out.println(s + "\t￥" + price + "\t数量：" + num + "\t合计￥" + total);
                System.out.println("是否继续(y/n)");
                str = sc.next();
            } else {
                System.out.println("是否继续(y/n)");
                str = sc.next();
            }
        } while (str.equals("y"));
        System.out.println("折扣：" + dis);
        System.out.println("应付金额：" + (total * dis));
        System.out.print("实付金额：");
        int sum = sc.nextInt();
        System.out.println("找钱：" + (sum - (total * dis)));
    }


    /**
     * 客户信息管理菜单
     */
    public void showCustMeun() {
        System.out.println("我行我素购物管理系统>客户信息管理");
        System.out.println("\t\t1.显示所有客户信息\n");
        System.out.println("\t\t2.添加客户信息\n");
        System.out.println("\t\t3.修改客户信息\n");
        System.out.println("\t\t4.查询客户信息");
        System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
        System.out.print("\n请选择，输入数字:");
        int i = 0;
        do {
            i = sc.nextInt();
            switch (i) {
                case 1:
                    i = -1;
                    break;
                case 2:
                    showCustMeun2();
                    i = -1;
                    break;
                case 3:
                    showCustMeun3();
                    i = -1;
                    break;
                case 4:
                    showCustMeun4();
                    i = -1;
                    break;
                default:
            }
        } while (i != -1);
    }

    /**
     * 添加用户信息
     */
    public void showCustMeun2() {
        System.out.print("我行我素购物管理系统>");
        System.out.print(" 客户信息管理>");
        System.out.print("添加客户信息\n\n");

        for (int i = 1; i <= 3; i++) {
            System.out.print("请输入会员号(<4位整数>):");
            int num = sc.nextInt();
            System.out.print("请输入会员生日(月/日<用两位数表示>):");
            String bir = sc.next();
            System.out.print("请输入会员积分:");
            int inte = 0;
            if (sc.hasNextInt()) {
                inte = sc.nextInt();
            } else {
                System.out.println("积分输入错误！请再次录入");
                continue;
            }
            System.out.println("已录入的会员信息：");
            if (num >= 1000 && num <= 9999) {
                System.out.print(num + "\t");
            } else {
                System.out.println("会员号不合法！");
                System.out.println("录入失败！请再次录入\n");
                continue;
            }
            if (bir.matches(s)) {
                System.out.print(bir + "\t");
            } else {
                System.out.println("生日信息不合法！");
                System.out.println("录入失败,请再次录入");
                continue;
            }
            System.out.println(inte + "\n");
        }
    }

    /**
     * 修改客户信息
     */
    public void showCustMeun3() {

        String sname;
        String mark;
        for (int i = 0; i < 10; i++) {
            System.out.print("请输入客户的姓名：");
            sname = sc.next();
            cus.addName(sname);
            System.out.print("继续输入吗？（y/n）:");
            mark = sc.next();
            if (mark.equals("y")) {
                continue;
            } else if (mark.equals("n")) {
                break;
            } else {
                System.out.println("输入错误！");
                break;
            }
        }
        cus.showName();

        System.out.print("\n请输入旧的客户姓名：");
        String oldName = sc.next();
        System.out.print("\n请输入新的客户姓名：");
        String newName = sc.next();
        System.out.println("*******修改结果*******");
        if (cus.editName(oldName, newName)) {
            System.out.println("找到并修改成功！");
        } else {
            System.out.println("修改失败！");
        }
    }

    /**
     * 查询客户信息
     */
    public void showCustMeun4() {
        cus.showName();
    }


    /**
     * 显示真情回馈菜单
     */
    public void showSendGMeun() {
        System.out.println("我行我素购物管理系统>真情回馈");
        System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
        System.out.println("\t\t1.幸运大放送\n");
        System.out.println("\t\t2.幸运抽奖\n");
        System.out.println("\t\t3.生日问候\n");
        System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
        System.out.print("\n请选择，输入数字:");
        int i = 0;
        do {
            System.out.print("\n请选择，输入数字:");
            i = sc.nextInt();
            switch (i) {
                case 1:
                    showSendGMeun1();
                    i = -1;
                    break;
                case 2:
                    showSendGMeun2();
                    i = -1;
                    break;
                case 3:
                    showSendGMeun3();
                    i = -1;
                    break;
                default:
                    break;
            }
        } while (i != -1);

    }

    /**
     * 幸运大放送
     */
    public void showSendGMeun1() {
        System.out.println("执行幸运大放送");
    }

    /**
     * 幸运抽奖
     */
    public void showSendGMeun2() {
        String str = null;           //判断是否继续
        String user = null;          //用户名
        String password = null;       //密码
        int max = 9999;
        int min = 1000;
        boolean mark = false;    //判断注册是否成功
        boolean mark1 = false;    //判断登录是否成功
        do {
            System.out.println("*****欢迎进入奖客富翁系统*****");
            System.out.println("\t\t1.注册");
            System.out.println("\t\t2.登录");
            System.out.println("\t\t3.抽奖");
            System.out.println("*****************************");
            System.out.print("请选择菜单：");
            int n = sc.nextInt();
            switch (n) {
                case 1:
                    System.out.println("奖客富翁系统> 注册");
                    System.out.println("请填写个人注册信息：");
                    System.out.print("用户名：");
                    user = sc.next();
                    System.out.print("密码：");
                    password = sc.next();
                    if (user != null && password != null) {
                        mark = true;
                        System.out.println("注册成功，请记住您的会员卡号");
                        Random random = new Random();
                        int cardNumber = (int) (random.nextDouble() * (max - min)) + min;
                        System.out.println("用户名\t\t密码\t\t会员卡号");
                        System.out.println(user + "\t\t" + password + "\t\t" + cardNumber);
                    } else {
                        System.out.println("注册失败！");
                        mark = false;
                    }
                    break;
                case 2:
                    System.out.println("奖客富翁系统> 登录");
                    if (mark == true) {
                        for (int i = 1; i <= 3; i++) {
                            System.out.print("请输入用户名：");
                            String u = sc.next();
                            System.out.print("请输入密码：");
                            String p = sc.next();
                            if (u.equals(user) && p.equals(password)) {
                                System.out.println("欢迎您，" + user);
                                mark1 = true;
                                break;
                            } else if (i < 3) {
                                System.out.println("用户名或密码错误，请重新输入：");
                            } else {
                                System.out.println("超过三次，登录失败！");
                            }
                        }
                    } else {
                        System.out.println("请先注册！");
                        mark1 = false;
                    }
                    break;
                case 3:
                    if (mark1 == true) {
                        System.out.println("奖客富翁系统> 抽奖");
                        System.out.print("请输入您的卡号：");
                        int number = 0;
                        if (sc.hasNextInt()) {
                            number = sc.nextInt();
                        } else {
                            System.out.println("输入错误！请输入数字");
                        }
                        int[] cn = new int[5];
                        for (int i = 0; i < cn.length; i++) {
                            cn[i] = (int) (Math.random() * (max - min)) + min;
                        }
                        System.out.print("本日的幸运数字为：");
                        for (int num : cn) {
                            System.out.print(num + " ");
                        }
                        for (int i = 0; i < cn.length; i++) {
                            if (number == cn[i]) {
                                System.out.println("您是本日幸运会员！");
                            } else if (i == cn.length - 1) {
                                System.out.println("\n抱歉！您不是本日幸运会员！");
                            }
                        }
                    } else {
                        System.out.println("请先登录！");
                    }

                    break;
                default:
                    System.out.println("您的输入有误！");
            }
            System.out.print("继续吗？(y/n)");
            str = sc.next();
        } while (str.equals("y"));

        System.out.println("\n幸运抽奖退出，谢谢使用！");

    }

    /**
     * 生日问候
     */
    public void showSendGMeun3() {
        System.out.println("生日问候");
    }

}
